package com.china.great.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.china.great.system.dao.SystemUserDao;
import com.china.great.system.entity.SystemLoginUser;
import com.china.great.system.entity.SystemUser;
import com.china.great.system.service.SystemUserService;
import com.china.great.system.util.JwtUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;


/**
 * @Author: Liu HongYang
 * @DateTime: 2020/7/13 9:09 下午
 * @Description: TODO
 */
@Service
public class SystemUserServiceImpl implements SystemUserService {
    private static final Logger logger = LoggerFactory.getLogger(SystemUserServiceImpl.class);


    @Autowired
    private SystemUserDao systemUserDao;

    private final AuthenticationManager authenticationManager;
    private final UserDetailsService userDetailsService;
    private final JwtUtils jwtTokenUtil;

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @Autowired
    public SystemUserServiceImpl(AuthenticationManager authenticationManager, @Qualifier("CustomUserDetailsService") UserDetailsService userDetailsService, JwtUtils jwtTokenUtil, SystemUserDao authMapper) {
        this.authenticationManager = authenticationManager;
        this.userDetailsService = userDetailsService;
        this.jwtTokenUtil = jwtTokenUtil;
        this.systemUserDao = authMapper;
    }

    @Override
    public IPage<SystemUser> getPageUserList(Page<?> page) {
        return systemUserDao.getPageUserList(page);
    }

    @Override
    public SystemLoginUser getUser(String username) {
        return systemUserDao.getUser(username);
    }

    @Override
    public String login(String username, String password) {
        //用户验证
        logger.debug("2=============进入到systemuserimpl的login方法，username跟password为="+username+":"+password);
        final Authentication authentication = authenticate(username, password);
        //存储认证信息
        SecurityContextHolder.getContext().setAuthentication(authentication);
        //生成token
        logger.info("2.1========准备生成token");
        final SystemLoginUser userDetail = (SystemLoginUser) authentication.getPrincipal();
        final String token = this.tokenHead +" "+ jwtTokenUtil.generateAccessToken(userDetail);
        //存储token
        jwtTokenUtil.putToken(username, token);
        logger.info("7========token生成完毕，并且put完毕");
        return token;
    }

    private Authentication authenticate(String username, String password) {
        //该方法会去调用userDetailsService.loadUserByUsername()去验证用户名和密码，如果正确，则存储该用户名密码到“security 的 context中”
        return authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
    }

}
